package com.mxpio.erp.inventory.service.impl;

import com.mxpio.erp.common.technology.entity.Item;
import com.mxpioframework.jpa.JpaUtil;
import com.mxpioframework.jpa.query.Criteria;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import com.mxpio.erp.common.inventory.entity.InvFlow;
import com.mxpio.erp.common.inventory.service.InvFlowService;
import com.mxpioframework.system.service.impl.BaseServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class InvFlowServiceImpl extends BaseServiceImpl<InvFlow> implements InvFlowService {
    @Override
    @Transactional
    public Page<InvFlow> listPageWithRL(Class<InvFlow> invFlowClass, Pageable pageAble, Criteria criteria) {//
        Page<InvFlow> invFlows = this.listPage(invFlowClass, pageAble, criteria);
        List<InvFlow> content = invFlows.getContent();
        if (content.size()>0){
            Map<String, Item> itemMap = new HashMap<>();
            for (InvFlow flow:content){
                Item item = null;
                if (itemMap.containsKey(flow.getItemCode())){
                    item = itemMap.get(flow.getItemCode());
                }else{
                    try {
                        item = JpaUtil.linq(Item.class).idEqual(flow.getItemCode()).findOne();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    if (item!=null){
                        itemMap.put(flow.getItemCode(),item);
                    }
                }
                if (item!=null){
                    flow.setItemName(item.getItemName());
                    flow.setItemSpec(item.getItemSpec());
                    flow.setUnitCode(item.getUnitCode());
                    flow.setItemGroupCode(item.getItemGroupCode());
                }
            }
        }
        return invFlows;
    }
}
